package com.ruike.alisurface.utils;

import android.text.TextUtils;

/**
 * 串口检验位类
 */
public class SerialsVerify {
    /**
     * 获取BCC校验码 异或校验
     *
     * @param data
     * @param start 开始位置0
     * @return
     */
    public static String getBCC(byte[] data, int start) {
        String ret = "";
        byte BCC[] = new byte[1];
        for (int i = start; i < data.length; i++) {
            if (i == data.length) {
                break;
            }
            BCC[0] = (byte) (BCC[0] ^ data[i]);
        }
        String hex = Integer.toHexString(BCC[0] & 0xFF);
        if (hex.length() == 1) {
            hex = '0' + hex;
        }
        ret += hex.toUpperCase();
        return ret;
    }

    /**
     * CRC16计算产生校验码
     *
     * @param data 需要校验的数据
     * @return 校验码
     */
    public static String Make_CRC(byte[] data) {
        byte[] buf = new byte[data.length];// 存储需要产生校验码的数据
        for (int i = 0; i < data.length; i++) {
            buf[i] = data[i];
        }
        int len = buf.length;
        int crc = 0xFFFF;//16位
        for (int pos = 0; pos < len; pos++) {
            if (buf[pos] < 0) {
                crc ^= (int) buf[pos] + 256; // XOR byte into least sig. byte of
                // crc
            } else {
                crc ^= (int) buf[pos]; // XOR byte into least sig. byte of crc
            }
            for (int i = 8; i != 0; i--) { // Loop over each bit
                if ((crc & 0x0001) != 0) { // If the LSB is set
                    crc >>= 1; // Shift right and XOR 0xA001
                    crc ^= 0xA001;
                } else
                    // Else LSB is not set
                    crc >>= 1; // Just shift right
            }
        }
        String c = Integer.toHexString(crc);
        if (c.length() == 4) {
            c = c.substring(2, 4) + c.substring(0, 2);
        } else if (c.length() == 3) {
            c = "0" + c;
            c = c.substring(2, 4) + c.substring(0, 2);
        } else if (c.length() == 2) {
            c = "0" + c.substring(1, 2) + "0" + c.substring(0, 1);
        }
        return c;
    }

    /**
     * 计算反码
     *
     * @param trueCode 原码
     * @return
     */
    public static long getOnesComplementCode(long trueCode) {
        long result = 0;
        StringBuilder sb = new StringBuilder();
        String trueBinaryStr = Long.toBinaryString(trueCode);
        if (!TextUtils.isEmpty(trueBinaryStr)) {
            String[] list = trueBinaryStr.split("");
            for (int i = 0; i < list.length; i++) {
                String item = list[i];
                if (item.equals("0")) {
                    sb.append("1");
                } else if (item.equals("1")) {
                    sb.append("0");
                }
            }
            String resultStr = sb.toString();
            if (!TextUtils.isEmpty(resultStr)) {
                result = Long.valueOf(resultStr, 2);
            }
        }
        return result;
    }

    // 十六进制转二进制
    public String HToB(String a) {
        String b = Integer.toBinaryString(Integer.valueOf(toD(a, 16)));
        return b;

    }

    // 二进制转十六进制
    public String BToH(String a) {
// 将二进制转为十进制再从十进制转为十六进制
        String b = Integer.toHexString(Integer.valueOf(toD(a, 2)));
        return b;
    }

    // 任意进制数转为十进制数
    public String toD(String a, int b) {//---------------------------a为16进制，b=16；三
        int r = 0;
        for (int i = 0; i < a.length(); i++) {
            r = (int) (r + formatting(a.substring(i, i + 1))
                    * Math.pow(b, a.length() - i - 1));
        }
        return String.valueOf(r);
    }

    // 将十六进制中的字母转为对应的数字
    public int formatting(String a) {
        int i = 0;
        for (int u = 0; u < 10; u++) {
            if (a.equals(String.valueOf(u))) {
                i = u;
            }
        }
        if (a.equals("A")) {
            i = 10;
        }
        if (a.equals("B")) {
            i = 11;
        }
        if (a.equals("C")) {
            i = 12;
        }
        if (a.equals("D")) {
            i = 13;
        }
        if (a.equals("E")) {
            i = 14;
        }
        if (a.equals("F")) {
            i = 15;
        }
        return i;
    }


    // 将十进制中的数字转为十六进制对应的字母
    public String formattingH(int a) {
        String i = String.valueOf(a);

        switch (a) {


            case 10:
                i = "A";

                break;

            case 11:
                i = "B";

                break;

            case 12:
                i = "C";

                break;

            case 13:
                i = "D";

                break;

            case 14:
                i = "E";

                break;

            case 15:
                i = "F";

                break;

        }
        return i;
    }
}
